package com.nobug;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;


/**
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2022-12-26
 */
public class ImageHistogramUtil {

    public static void main(String[] args) throws IOException {

        String path = "C:\\Users\\Administrator\\Desktop\\testImage\\";
        String filename = "6.png";
        System.out.println(match(new File(path + filename), new File(path + "2.png")));
        System.out.println(match(new File(path + filename), new File(path + "3.png")));
        System.out.println(match(new File(path + filename), new File(path + "4.png")));
        System.out.println(match(new File(path + filename), new File(path + "5.png")));

    }

    private static final ImageHistogram histogram = new ImageHistogram();
    private static final File file = new File(GlobalConfig.IMAGE_MODE); //样品

    /**
     * 识别图片中的数字 返回 数字 无数字就返回 0
     * @param image
     * @return
     */
    public static int getNum(BufferedImage image) {
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".png");
            }
        });
        for (File file1 : files) {
            double score  = 0;
            try {
                score = histogram.match(image, file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
//            System.out.println(file1.getName()+"比对本地样品：: " + score);
            if(score > 0.998){
//                System.out.println(file1.getName());
                String name = file1.getName();
//                System.out.println(Integer.parseInt(name.split("\\.")[0]));
                String nameNum = name.split("\\.")[0];
                String num = nameNum.split(",")[0];
                return Integer.parseInt(num);
            }
        }
        return 0;
    }


    public static double match(BufferedImage image, BufferedImage file) {
        double score = 0;
        try {
            score = histogram.match(image, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return score;
    }

    public static double match(BufferedImage image, File file) {
        double score = 0;
        try {
            score = histogram.match(image, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return score;
    }

    public static double match(File image, File file) {
        double score = 0;
        try {
            score = histogram.match(image, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return score;
    }
}
